package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.vo.BorrowerAuthVO;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.result.ResponseVo;
import com.atguigu.srb.util.JwtUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2024-04-15
 */
@RestController
@RequestMapping("/api/core/borrower")
public class ApiBorrowerController {
    @Resource
    private BorrowerService borrowerService;
    @ApiOperation("保存借款人信息")
    @PostMapping("/save")
    public ResponseVo save(@RequestBody BorrowerAuthVO borrowerAuthVO, @RequestHeader("token") String  token){
        borrowerService.saveBorrowerInfo(borrowerAuthVO,token);
        return ResponseVo.ok().message("提交成功");
    }

    @ApiOperation("获取申请借款资格的状态")
    @GetMapping("/status")
    public ResponseVo getStatus(@RequestHeader("token") String token){
        Long userId = JwtUtils.getUserId(token);
        Borrower borrower = borrowerService.getOne(Wrappers.lambdaQuery(Borrower.class).eq(Borrower::getUserId, userId));
        if(borrower== null){
            return ResponseVo.ok().addData("item",0);
        }
        Integer status = borrower.getStatus();
        return ResponseVo.ok().addData("item",status);
    }
}

